﻿using Model;
using Model.Block;
using log4net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BehaviorModel.Block.Summator
{
    public class Summator : ISummator
    {
        #region Fields

        private static readonly ILog logger = LogManager.GetLogger(typeof(Summator));

        private IReceiverBlock _receiverBlock;

        #endregion

        #region Properties

        #region IBlock

        public Guid Id { get; private set; }
        public BlockType Type { get { return BlockType.Summator; } }

        #endregion

        public SummatorStorage Storage { get; private set; }

        #endregion

        public Summator()
        {
            Id = Guid.NewGuid();
            Storage = new SummatorStorage();
        }

        #region Methods

        #region IBlock

        public void Initialize()
        {
        }

        #endregion

        #region IReceiverBlock

        public void Receive(Request request)
        {
            Send(request);
        }

        #endregion

        #region ISenderBlock

        public void ConnectReceiverBlock(IReceiverBlock block)
        {
            logger.Info("Summator is Adding Recieving Block " + block.GetType().ToString() + " with id: " + block.Id);
            _receiverBlock = block;
        }

        public void DisconnectReceiverBlock(IReceiverBlock receiverBlock)
        {
            _receiverBlock = null;
        }

        public void Send(Request request)
        {
            _receiverBlock.Receive(request);
            Storage.CountOfPassed++;
        }

        #endregion

        #endregion

    }
}
